package com.dong.services;

import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class CaptchaService
{
    private final Long TENCENT_CAPTCHA_TYPE = 9L;
    private final String appSecretKey = "0WAPfH3JyoYSEkB23q5ouxC43";
    private final String captchaAppId = "189982985";
//    private final String appSecretKey = "N3zeaGCg2KrV43OCRDqMKoVte";
//    private final Long captchaAppId = 189921598L;



    private final String TENCENT_CAPTCHA_SUCCESS_CODE = "1";
    private final String TENCENT_CAPTCHA_NOT_MATCH = "7";
    public String tencentCaptchaCheck(String ticket, String randStr, String username) {
        //Credential credential = new Credential(secretId, secretKey);
        //Credential credential = new Credential("189921598", "N3zeaGCg2KrV43OCRDqMKoVte");
        Credential credential = new Credential("IKID63pOk05zfy63oMisrcAXikaM0UqIl4MY", "GMI1azEYhiZDerqOoJza2HNK5d4LSM1U");
        CaptchaClient client = new CaptchaClient(credential, null);
        DescribeCaptchaResultRequest request = new DescribeCaptchaResultRequest();
        request.setCaptchaType(TENCENT_CAPTCHA_TYPE);
        request.setTicket(ticket);
        request.setUserIp("");
        request.setCaptchaAppId(Long.parseLong(captchaAppId));
        request.setAppSecretKey(appSecretKey);
        request.setRandstr(randStr);
        try {
            DescribeCaptchaResultResponse response = client.DescribeCaptchaResult(request);
            switch (response.getCaptchaCode().toString()) {
                case TENCENT_CAPTCHA_SUCCESS_CODE:
                    System.out.println("success");
                    // 判断是否恶意攻击
//                    if (response.getEvilLevel() == 100) {
//                        sendTencentCaptchaErrorMsg("Tencent captcha exception. Evil level = 100, remoteIp = " + SecurityUtils.getRemoteIp());
//                        throw new BaseBizException(ErrorCodeEnum.RECAPTCHA_INVALID);
//                    }
                    return "success";
                case TENCENT_CAPTCHA_NOT_MATCH:
                    return "captcha not match";
//                case TENCENT_CAPTCHA_SECRET_KEY_ERROR_CODE:
                    //String content = String.format("Tencent captcha exception.\n captcha code = %s, captcha msg = %s", response.getCaptchaCode(), response.getCaptchaMsg());
                    //sendTencentCaptchaErrorMsg(content);
                   // throw new BaseBizException(ErrorCodeEnum.RECAPTCHA_INVALID);
                default:
                    return  "captcha error";
                   // throw new BaseBizException(ErrorCodeEnum.RECAPTCHA_INVALID);
            }
        } catch (TencentCloudSDKException e) {
            log.error("TencentCloudSDKException",e);
            //log.error("Tencent captcha exception. msg = {}", e.toString());
//            sendTencentCaptchaErrorMsg("Tencent captcha exception. msg = " + e);
//            throw new BaseBizException(ErrorCodeEnum.RECAPTCHA_INVALID);
        } catch (Exception e) {
            log.error("Exception");
//            log.error("Tencent captcha exception.", e);
//            throw new BaseBizException(ErrorCodeEnum.RECAPTCHA_INVALID);
        }
        return  "captcha error";
    }


}